package com.eling.elcms.travel.webapp.controller;

import com.eling.elcms.core.dao.hibernate.search.SearchCondition;
import com.eling.elcms.core.webapp.controller.BaseFormController;
import com.eling.elcms.core.webapp.controller.Msg;
import com.eling.elcms.core.webapp.spring.ModelFilling;
import com.eling.elcms.travel.model.CheckInImplement.DataSource;
import com.eling.elcms.travel.model.CheckOutDeal;
import com.eling.elcms.travel.model.view.ChangeRoomSettlementView;
import com.eling.elcms.travel.model.view.CheckOutDealSaveView;
import com.eling.elcms.travel.service.ICheckOutDealManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
public class CheckOutDealController extends BaseFormController{

	@Autowired
	private ICheckOutDealManager checkOutDealManager;
	
	@RequestMapping("/api/checkoutdeal/save")
    @ResponseBody
	public CheckOutDeal save(@ModelAttribute@ModelFilling CheckOutDeal cond){
		return checkOutDealManager.save(cond);
	}
	
	@RequestMapping("/api/checkoutdeal/{pkCheckOutDeal}/delete")
	@ResponseBody
	public Msg delete(@PathVariable Long pkCheckOutDeal){
		checkOutDealManager.remove(pkCheckOutDeal);
		return new Msg("删除成功");
	}
	
	@RequestMapping("/api/checkoutdeal/query")
    @ResponseBody
	public List<CheckOutDeal> query(@ModelAttribute CheckOutDeal cond,
			@RequestParam(value="paymentMethod",required = false)String paymentMethod,
			@RequestParam(value="source",required = false)String source){
		List<CheckOutDeal> returnList = new ArrayList<>();
		if(paymentMethod == null){
			returnList = checkOutDealManager.query(cond);
		}else{
			returnList = checkOutDealManager.queryByPaymentMethod(cond,paymentMethod);
		}
		//由于In不好使，所以这样写。
		if(source == null){
			return returnList;
		}else {
			List<CheckOutDeal> returns = new ArrayList<>();
			for (CheckOutDeal checkOutDeal : returnList) {
				if(checkOutDeal.getCheckInImplement().getDataSource() == DataSource.NonMember || 
						checkOutDeal.getCheckInImplement().getDataSource() == DataSource.VisitDoorFIT){
					returns.add(checkOutDeal);
				}
			}
			return returns;
		}
	}
	
	@RequestMapping("/api/checkoutdeal/search*")
	@ResponseBody
	public List<CheckOutDeal> search(@ModelAttribute SearchCondition sc,@ModelAttribute CheckOutDeal cond){
		sc.setQueryCondition(cond);
		return checkOutDealManager.search(sc);
	}
	
	/**
	 * 散客退房结算
	 * @param view
	 * @return
	 * @author zhoulin
	 */
	@RequestMapping("/api/checkoutdeal/saveall")
    @ResponseBody
	public CheckOutDeal saveAll(@ModelAttribute CheckOutDealSaveView view){
		return checkOutDealManager.saveAll(view);
	}
	
	/**
	 * 散客退房结算中调整房间记录
	 */
	@RequestMapping("/api/checkoutdeal/querychangeroom")
	@ResponseBody
	public List<ChangeRoomSettlementView> queryChangeRoom(@RequestParam(value="pkCIImplement")Long pkCIImplement){
		return checkOutDealManager.queryChangeRoom(pkCIImplement);
	}
	
	//api/checkoutdeal/querybypkciimplement
	@RequestMapping("/api/checkoutdeal/querybypkciimplement")
	@ResponseBody
	public CheckOutDeal queryBypkCiimplement(@RequestParam(value="pkCIImplement")Long pkCIImplement){
		return checkOutDealManager.queryBypkCiimplement(pkCIImplement);
	}
}
